home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_09_12 / 9n12084a < prev    next >
Text File  |  1991-07-25  |  7KB  |  226 lines

  1. /** LISTING 4 ** UART.H ******************************
  2.      Structures and defines for UART asynch driver 
  3. *****************************************************/
  4.  
  5.  
  6. /*****************************************************
  7.               DEFINES AND MACROS
  8. *****************************************************/
  9.  
  10. #if UARTMAIN==1
  11. #define UART_VTYPE 
  12. #else
  13. #define UART_VTYPE extern
  14. #endif
  15.  
  16.  
  17. /* Returns true if x indicates CTS high */
  18. #define IS_CTS(x)  ( (x) & 0x10 )   
  19.  
  20. /* Returns true if x indicates DSR high */
  21. #define IS_DSR(x)  ( (x) & 0x20 )   
  22.  
  23. /* Returns true if x indicates RING high */
  24. #define IS_RING(x) ( (x) & 0x40 )   
  25.  
  26. /* Returns true if x indicates DCD high */
  27. #define IS_DCD(x)  ( (x) & 0x80 )   
  28.  
  29.    /*** Port address offset defines ***/
  30. #define TRANSMIT_REGISTER 0
  31. #define RECEIVE_REGISTER 0
  32. #define DIV_LATCH_LOW_BYTE 0
  33. #define DIV_LATCH_HIGH_BYTE 1
  34. #define INTERRUPT_ENABLE_REGISTER 1
  35. #define INTERRUPT_IDENT_REGISTER 2
  36. #define FIFO_CONTROL_REGISTER 2            
  37. #define LINE_CONTROL_REGISTER 3
  38. #define MODEM_CONTROL_REGISTER 4
  39. #define LINE_STATUS_REGISTER 5
  40. #define MODEM_STATUS_REGISTER 6
  41.  
  42.    /*** Defines for fifo control register ***/
  43. /* To reset the FIFO at the end of program */
  44. #define FCR_RESET_FIFO         0x00        
  45.  
  46. /* To enable the FIFO */
  47. #define FCR_ENABLE_FIFO        0x01        
  48.  
  49. /* To flush the receive FIFO  */
  50. #define FCR_FLUSH_RCV_FIFO     0x02        
  51.  
  52. /* To flush the transmit FIFO  */
  53. #define FCR_FLUSH_XMIT_FIFO    0x04        
  54.  
  55. /* Receive interrupt triggered when 1 byte in FIFO */
  56. #define FCR_RCV_TRIG_1         0x00        
  57.  
  58. /* Receive interrupt triggered when 4 bytes in FIFO */
  59. #define FCR_RCV_TRIG_4         0x40        
  60.  
  61. /* Receive interrupt triggered when 8 bytes in FIFO */
  62. #define FCR_RCV_TRIG_8         0x80        
  63.  
  64. /* Receive interrupt triggered when 14 bytes in FIFO */
  65. #define FCR_RCV_TRIG_14        0x0C        
  66.  
  67.  
  68. /* Enable the FIFO and set
  69.    it to generate rcv
  70.    interrupts only when fifo
  71.    has more than 8 characters */
  72. #define FIFO_ENABLE (FCR_ENABLE_FIFO | FCR_RCV_TRIG_8)
  73.  
  74.  
  75.  /* If masking Interrupt Identification Register 
  76.    value with this value equals this value,
  77.    then FIFO has been enabled. */
  78. #define IIR_FIFO_ENABLED       0xC0        
  79.                                            
  80. /* Mask value when checking interrupt status.                                          
  81.    Masking the IIR with this value                                           
  82.    makes it look the same regardless of FIFO                                           
  83.    being on or off. */                                           
  84. #define IIR_MASK   0x07                     
  85.                                            
  86. /* Address of 8259 PIC mask register for IRQ 0 - 7 */                                           
  87. #define R8259A_MASK 0x21            
  88.  
  89. /* Address of 8259 PIC control register for IRQ 0 - 7 */
  90. #define R8259A_CONTROL 0x20         
  91.  
  92. /* Maximum number of characters to transmit
  93.    to FIFO at one time. */
  94. #define MAX_TRANFIFO 16             
  95.                                     
  96. /* Maximum number of characters to read from
  97.    the FIFO when the FIFO is below the threshold */
  98. #define MAX_RCVFIFO  12             
  99.                                     
  100.  
  101. /* Size of transmit and receive buffers */
  102. #define BUFFER_SIZE 2048
  103.  
  104.  
  105.  /*** Flag masks for received characters.
  106.       If error is indicated, character may not
  107.       be valid. If high bit is set, it indicates
  108.       an error in the API function. ***/
  109. #define GOOD_CHARACTER     0x0000
  110. #define OVERRUN_ERROR      0x0100
  111. #define PARITY_ERROR       0x0200
  112. #define FRAMING_ERROR      0x0400
  113. #define BREAK_SIGNAL       0x0800
  114. #define RING_INDICATION    0x1000
  115. #define BUFFER_OVERRUN     0x2000
  116.  
  117. /*****************************************************
  118.               STRUCTURE DEFINITIONS
  119. *****************************************************/
  120.  
  121. /* Structure entry giving information about each port
  122.    in the system.  */
  123. struct t_port_info 
  124. {                 
  125.    /* Circular transmit and receive buffers
  126.       for this port */
  127.    char a_transmit_buffer[BUFFER_SIZE];
  128.    char a_receive_buffer[BUFFER_SIZE*2];
  129.  
  130.    /* Head and tail indices within transmit and
  131.       receive buffers.  Head always points to 
  132.       next available slot for character to be
  133.       placed in.  Tail always points to next
  134.       character to be retreived.  If head and
  135.       tail are equal, then buffer is empty.  For 
  136.       receive buffer, index is a word index.
  137.       Receive characters are ordered in pairs 
  138.       with the first byte being the character and the 
  139.       second byte being a bitmapped flag. */
  140.    unsigned int tx_head;
  141.    unsigned int tx_tail;
  142.    unsigned int rx_head;
  143.    unsigned int rx_tail;
  144.  
  145.    /* Physical base I/O address port's UART chip */
  146.    unsigned int base_address;    
  147.  
  148.    /* Baud Rate */
  149.    unsigned long baud_rate;
  150.  
  151.    /* Number of bits per data character (5-8) */
  152.    unsigned char data_bits;
  153.  
  154.    /* Parity (0=None, 1=Odd, 3=Even) */
  155.    unsigned char parity;
  156.  
  157.    /* Number of stop bits (1 or 2) */
  158.    unsigned char stop_bits;
  159.                                            
  160.    /* Number of characters read from FIFO without a 
  161.       receive interrupt. */
  162.    unsigned char non_int_chars;            
  163.  
  164.    /* Number of characters that can be transmitted on a
  165.       single transmitter empty interrupt */
  166.    unsigned char max_tx_chars;
  167.  
  168.  
  169.    /*** Bit fields are all 1 for yes, 0 for no. ***/
  170.  
  171.    /* Will driver obey RTS/CTS protocol signals on 
  172.       this port? */
  173.    unsigned obey_rts_cts:1;                
  174.  
  175.    /*** Bit fields representing signal states - 
  176.         1 = On, 0 = Off ***/
  177.  
  178.    /* DataSet Ready */
  179.    unsigned dsr_state:1;
  180.  
  181.    /* Clear To Send */
  182.    unsigned cts_state:1;
  183.  
  184.    /* Data Carrier Detect */
  185.    unsigned dcd_state:1;
  186.  
  187. } ;
  188.  
  189.  
  190. /*****************************************************
  191.               GLOBAL VARIABLES
  192. *****************************************************/
  193.  
  194. /* Number of asynchronous serial ports configured
  195.    system-wide. */
  196. UART_VTYPE unsigned char g_num_ports;           
  197.  
  198. /* Pointer to calloc'ed array of port information
  199.    structures.  One for each configured port in the 
  200.    system. During initialization each port will be 
  201.    assigned a sequential number from 1 to g_num_ports
  202.    (0 is not used) which will be its position within 
  203.    this array. Ports are accessed as 
  204.    gp_port_info[port_nbr-1]. */                                                
  205. UART_VTYPE struct t_port_info *gp_port_info;    
  206.  
  207. /* IRQ (Interrupt ReQuest line) number used by 
  208.    UART(s). Must be between 3 and 7 */
  209. UART_VTYPE unsigned int g_uart_irq;
  210.  
  211.  
  212. /*****************************************************
  213.               FUNCTION PROTOTYPES
  214. *****************************************************/
  215.  
  216. void interrupt far Asynch_driver(void);
  217. int Test_for_asynch_port( unsigned int port_address, 
  218.                           unsigned int irq_level );
  219. void interrupt far Catch_asynch_test_interrupt(void);
  220. int Init_UART(void);
  221. void Exit_UART(void);
  222. int Send_char(unsigned int port_number,
  223.                unsigned char tx_char);
  224. int Read_char(unsigned int port_number);
  225.  
  226.